<?php
namespace app\model\crm;

use think\Model;

class Contact extends Model
{
    // 设置表名
    protected $name = 'crm_contact';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 定义字段类型
    protected $type = [
        'is_primary' => 'boolean',
        'status' => 'integer'
    ];
    
    // 关联客户
    public function customer()
    {
        return $this->belongsTo(Customer::class, 'customer_id');
    }
    
    // 关联审核记录
    public function reviews()
    {
        return $this->hasMany(Review::class, 'target_id')->where('review_type', 2);
    }
    
    // 获取联系人状态标签
    public function getStatusTextAttr($value, $data)
    {
        $status = [
            1 => '正常',
            2 => '待审核',
            3 => '已删除'
        ];
        return $status[$data['status']] ?? '未知';
    }
}